var _util = _interopRequireDefault(require("../../utils/util")), _user = _interopRequireDefault(require("../../utils/user")), _mobileUtil = require("./mobile-util");

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _defineProperty(e, t, o) {
    return t in e ? Object.defineProperty(e, t, {
        value: o,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[t] = o, e;
}

var loginCode, COUNTDOWN = 60, DEFAULT_BTN_TEXT = "验证手机", AGAIN_BTN_TEXT = "重新验证", mobileVerifyStatus = {
    DEFAULT: 0,
    VERIFY_SUCCESS: 1,
    VERIFY_FAIL: 2
}, countdown = 0, isFirstInput = !0;

Component({
    options: {
        multipleSlots: !0
    },
    properties: {
        enabledLabel: {
            type: Boolean,
            value: !0
        },
        enabledCodeLabel: {
            type: Boolean,
            value: !0
        },
        appName: {
            type: String,
            value: "card"
        },
        mobile: {
            type: String,
            value: "",
            observer: function(e) {
                var t = {};
                isFirstInput && (t = {
                    enabledMobileGet: !1,
                    customButton: !0,
                    mobileVerifyState: mobileVerifyStatus.VERIFY_SUCCESS
                }), t["form.mobile"] = e, this.setData(t), this.updateEnabledSendCode();
            }
        },
        codeLen: {
            type: String,
            value: 4
        },
        customButton: {
            type: Boolean,
            value: !1
        },
        isVerifyCode: {
            type: Boolean,
            value: !0
        },
        enabledMobileGet: {
            type: Boolean,
            value: !0,
            observer: function(e) {
                var t = this.data, o = t.form.fastverify, n = t.mobileVerifyState;
                o || n === mobileVerifyStatus.VERIFY_SUCCESS || !1 === e && this.setData({
                    showVcodeInput: !0
                });
            }
        },
        isShownSendBtn: {
            type: Boolean,
            value: !0
        }
    },
    data: {
        form: {
            mobile: "",
            code: ""
        },
        mobileVerifyStatus: mobileVerifyStatus,
        mobileVerifyState: mobileVerifyStatus.DEFAULT,
        enabledSendCode: !1,
        sendCodeButtonText: DEFAULT_BTN_TEXT,
        showVcodeInput: !1
    },
    ready: function() {
        isFirstInput = !1, this.preLogin();
    },
    detached: function() {
        isFirstInput = !0, countdown = 0, clearInterval(this.timer), clearInterval(this.countTimer);
    },
    methods: {
        preLogin: function() {
            console.log("prelogin"), _user.default.getWxLoginCode().then(function(e) {
                return loginCode = e.code;
            }), this.timer = setInterval(function() {
                _user.default.getWxLoginCode().then(function(e) {
                    return loginCode = e.code;
                });
            }, 27e4);
        },
        onInput: function(e) {
            var t = e.detail, o = t.name, n = t.value;
            n = n.trim(), this.setData(_defineProperty({}, "form.".concat(o), n)), "mobile" === o && this.updateEnabledSendCode(), 
            this.$eventEmit("input", {
                name: o,
                value: n
            });
        },
        sendCode: function() {
            var e = this, t = this.data.form.mobile, o = this.startCountdown();
            (0, _mobileUtil.sendMobileCode)(t).then(function() {
                e.setData({
                    showVcodeInput: !0,
                    isVerifyCode: !0
                });
            }).catch(function() {
                o(), e.updateSendCodeStatusByCountDown(), e.$eventEmit("fail", {
                    value: "短信发送失败，请重试"
                });
            });
        },
        updateEnabledSendCode: function() {
            var e = this.data.form, t = e.fastverify, o = e.mobile;
            this.setData({
                enabledSendCode: !t && (_util.default.isMobile(o) && 0 === countdown)
            });
        },
        updateSendCodeStatusByCountDown: function() {
            var e = 0 === countdown ? AGAIN_BTN_TEXT : "".concat(countdown, "S");
            this.setData({
                sendCodeButtonText: e
            }), 0 !== countdown && countdown !== COUNTDOWN || this.updateEnabledSendCode();
        },
        startCountdown: function() {
            var e, t = this;
            return countdown = COUNTDOWN, this.updateSendCodeStatusByCountDown(), e = this.countTimer = setInterval(function() {
                countdown--, t.updateSendCodeStatusByCountDown(), 0 === countdown && clearInterval(e);
            }, 1e3), function() {
                countdown = 0, clearInterval(e);
            };
        },
        bindMobileGet: function(e) {
            var o = this;
            this.$report("quick_get_mobile_btn");
            (0, _mobileUtil.resolverWxMobile)(e.detail, this.data.appName, loginCode).then(function(e) {
                var t = e.data.phoneNumber;
                o.setData({
                    enabledMobileGet: !1,
                    "form.mobile": t,
                    "form.fastverify": 1,
                    sendCodeButtonText: "验证成功",
                    mobileVerifyState: mobileVerifyStatus.VERIFY_SUCCESS
                }), o.$eventEmit("input", {
                    name: "mobile",
                    value: t
                });
            }, function() {
                o.$eventEmit("fail", {
                    value: "快速获取失败，请手动输入"
                }), o.setData({
                    enabledMobileGet: !1,
                    showVcodeInput: o.data.isVerifyCode
                });
            });
        },
        verifyForm: function() {
            var e, t = this.data, o = t.form, n = t.showVcodeInput, i = t.codeLen;
            if (!o.fastverify) return o.mobile ? _util.default.isMobile(o.mobile) ? n && (e = o.code ? o.code.length === i ? "" : "请输入正确的验证码" : "请输入验证码") : e = "手机号格式不正确" : e = "请输入手机号", 
            e;
        },
        getFormData: function() {
            var e = this.verifyForm();
            return e ? this.$eventEmit("fail", {
                value: e
            }) : this.data.form;
        },
        $eventEmit: function(e, t) {
            this.triggerEvent(e, t, {});
        }
    }
});